package com.wlb.address.split.process;

import com.wlb.address.split.domain.AddrLevel;
import com.wlb.address.split.domain.BracketIncludeInfo;
import com.wlb.address.split.domain.Constants;
import com.wlb.address.split.domain.GeoTerm;
import com.wlb.address.split.rule.ExperienceRuleSet;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * User: xiaotong.dxt
 * Date: 13-8-8
 * Time: 上午11:47
 * To change this template use File | Settings | File Templates.
 */
public class DefaultAddressSpliter extends AddressSpliter {
    @Override
    public String addrStand(String address) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String adminAreaRepeat(String address) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String fetchBracketIncludeAddr(String address, Queue<BracketIncludeInfo> bracketIncludeInfos) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String wordSplitUnitNo(String address, Queue<GeoTerm> termOut) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    private boolean buildingNoDealWithPreResult(Queue<GeoTerm> termOut, String word) {
        return false;
    }

    @Override
    public String doFusion(String address, List<GeoTerm> termOut, FusionResultSet fusionResults) {

        boolean bFailed;

        //倒数第二个term的词性
        String strPreWordProp = "";

        for (int i = 0; i < termOut.size(); i++) {
            GeoTerm term = termOut.get(i);
            String strWordName = term.getWord();
            if (StringUtils.isBlank(strWordName))
                continue;
            //如果是两个字的情况
            if (strWordName.length() == 2) {
                //如果当前词是街道，则与fusion前面一个元素进行合并
                if (strWordName.equals("街道")) {
                    fusionResults.peekLast().strWordName += strWordName;
                    fusionResults.peekLast().nLevel = AddrLevel.AL_TOWN;
                    fusionResults.peekLast().nProp = Constants.PROP_TYPE_THINK;
                    continue;
                }
            } else {
                fusionResults.insertWordtoResultSet(strWordName, AddrLevel.AL_ADDR_INFO, 2);
                continue;
            }
            //如果关键字是道路
            if (ExperienceRuleSet.getInstance().roadSuffixSet.contains(strWordName)) {
                if (fusionResults.size() > 0) {
                    if (fusionResults.peekLast().chProp != 'G' && fusionResults.peekLast().chProp != 'H') {
                        AddrLevel nLevel = fusionResults.peekLast().nLevel;
                        //如果前面是POI或者什么的，则跟前面的合并成为道路
                        if (nLevel.getLevel() <= AddrLevel.AL_BIZ_ZONE.getLevel() || nLevel == AddrLevel.AL_POI) {
                            fusionResults.peekLast().strWordName += strWordName;
                            fusionResults.peekLast().nLevel = AddrLevel.AL_ROAD;
                            fusionResults.peekLast().nProp = Constants.PROP_TYPE_MATCH;
                            fusionResults.peekLast().ballowedTail = false;
                        } else {
                            fusionResults.insertWordtoResultSet(strWordName, AddrLevel.AL_ADDR_INFO, 2);
                        }
                        continue;

                    } else {
                        fusionResults.insertWordtoResultSet(strWordName, AddrLevel.AL_ADDR_INFO, 2);
                        continue;
                    }
                }
                //如果前面没元素，则不合并，供后续操作
            }
            //对门牌号进行处理
/*            boolean bFailded = buildingNoDealWithPreResult(termOut, wordSplitUnitNo());
            if (bFailded)
                continue;*/
            String strWordProp = term.getProp();
            //对于找不到属性的term，直接合并 ,直到下一个词词性不为空
            if (StringUtils.isBlank(strWordProp)) {
                strWordName = "";
                while (i < termOut.size()) {

                    GeoTerm tempTerm = termOut.get(i);
                    if (StringUtils.isNotBlank(tempTerm.getProp())) {
                        break;
                    }
                    strWordName += tempTerm;
                    i++;
                }
                fusionResults.insertWordtoResultSet(strWordName, AddrLevel.AL_ADDR_INFO, 3);
                continue;
            }

            //对F词性进行处理
            //F词性 办事处   77  调查队   77 分校   77
            if (strWordProp.equals("F")) {
                bFailed = WordProcessor.FProp_Deal(fusionResults, strWordName, false);
                if (bFailed) {
                    //操作成功 则continue;
                    continue;

                }
            }
            //如果词性带有H
            if (strWordName.contains("H")) {
                //比较复杂 等会儿再写 累感不爱
            }
            if (strWordName.equals("B")) {
                if (fusionResults.size() > 0) {
                    //特殊区特殊处理
                    bFailed = WordProcessor.AdminArea_SpecDistrict(fusionResults, strWordName);
                    if (bFailed)
                        continue;
                    if (fusionResults.peekLast().nLevel != AddrLevel.AL_ADDR_INFO || (fusionResults.peekLast().nLevel == AddrLevel.AL_ADDR_INFO && isAllowed_WordsMerge(strWordProp))) {
                        fusionResults.insertWordtoResultSet(strWordName, AddrLevel.AL_ADDR_INFO, 2);
                        continue;
                    } else {
                        //浙江省杭州市萧山区商业城商城中路教学楼[R+B]   --跟前面合并
                        fusionResults.peekLast().strWordName += strWordName;
                        fusionResults.peekLast().nProp = Constants.PROP_TYPE_THINK;
                        fusionResults.peekLast().nLevel = AddrLevel.AL_POI;
                        continue;
                    }
                } else {
                    fusionResults.insertWordtoResultSet(strWordName, AddrLevel.AL_ADDR_INFO, 2);
                    continue;
                }
            }
            //词性为复合词
            if (strWordProp.length() > 1) {
                //	RepeatWordPropCheck( strWordProp,LevelProp );无法理解 TODO
            }
            if (i > 0) {
                //获取倒数第二个的词性
                strPreWordProp = termOut.get(i - 1).getProp();
            }

            //J应该是居委会，村委会之类的
            if (fusionResults.size() > 0 && strWordProp.equals("J")) {
                if (fusionResults.peekLast().nLevel == AddrLevel.AL_TOWN
                        || fusionResults.peekLast().nLevel == AddrLevel.AL_VILLAGE
                        || fusionResults.peekLast().nLevel == AddrLevel.AL_BIZ_ZONE
                        || fusionResults.peekLast().nLevel == AddrLevel.AL_ROAD
                        || fusionResults.peekLast().nLevel == AddrLevel.AL_SUB_ROAD
                        || fusionResults.peekLast().nLevel == AddrLevel.AL_POI) {

                    WordProcessor.Village_Community(strWordName, fusionResults.peekLast());

                }
            }
            //然后对包含J的词性的操作 进行合并操作
            WordProcessor.JPro_Deal(fusionResults, strWordName);

        }  //循环尾


        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    //判断是否允许词条合并
    private boolean isAllowed_WordsMerge(String prop) {
        int nSize = prop.length();
        if (nSize < 1)
            return false;
        ////对于'2',H情况都不允许合并//// TODO 2是什么H是什么
        if (prop.equals("2") || prop.contains("H")) {
            return true;
        }
        return false;
    }
}
